package com.hk.housekeeping.service.impl;

import com.hk.housekeeping.entity.UserEntity;
import com.hk.housekeeping.mapper.UserMapper;
import com.hk.housekeeping.service.UserService;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     *添加用户信息
     * @param userEntity
     * @return 返回1代表添加成功601代表失败
     */
    @Override
    public ResponseEntity<Integer> addUser(UserEntity userEntity) {
        ResponseEntity<Integer> entity=null;
        int i = userMapper.addUser(userEntity);
        if (i==0){
            entity=ResponseEntity.error(ResponseCode.USER_ADD_ERROR);
        }else {
            entity=ResponseEntity.success(i);
        }
        return entity;
    }

    /**
     * 删除用户信息
     * @param uid
     * @return 返回1代表添加成功602代表失败
     */
    @Override
    public ResponseEntity<Integer> delUser(int uid) {
        ResponseEntity<Integer> entity=null;
        int i = userMapper.delUser(uid);
        if (i==0){
            entity=ResponseEntity.error(ResponseCode.USER_DEL_ERROR);
        }else {
            entity=ResponseEntity.success(i);
        }
        return entity;
    }

    /**
     * 修改用户信息
     * @param userEntity
     * @return 返回1代表添加成功603代表失败
     */
    @Override
    public ResponseEntity<Integer> modifyUser(UserEntity userEntity) {
        ResponseEntity<Integer> entity=null;
        int i = userMapper.modifyUser(userEntity);
        if (i==0){
            entity=ResponseEntity.error(ResponseCode.USER_MOD_ERROR);
        }else {
            entity=ResponseEntity.success(i);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Integer> updateUser(UserEntity userEntity) {
        ResponseEntity<Integer> entity = null;
        //1、先进行假删除
        int count1 = userMapper.delUser(userEntity.getUid());
        //2、重新插入新的记录
        UserEntity user = new UserEntity();
        user.setName(userEntity.getName());
        user.setPhone(userEntity.getPhone());
        user.setWechat(userEntity.getWechat());
        int count2 = userMapper.addUser(user);

        if (count1 != 0 && count2 != 0) {
            entity = ResponseEntity.success(1);
        } else {
            entity = ResponseEntity.error(ResponseCode.USER_MOD_ERROR);
        }

        return entity;
    }

    /**
     * 查询用户信息
     * @param uid
     * @return  用户信息
     */
    @Override
    public UserEntity getUser(int uid) {
        return userMapper.getUser(uid);
    }

    /**
     * 通过昵称或手机号查询
     * @param  fuzzy
     * @return 用户信息
     */
    @Override
    public List<UserEntity> getUserFuzzy(String fuzzy) {
        return userMapper.getUserFuzzy(fuzzy);
    }

    /**
     * 按时间范围查询
     * @param datetime1 开始时间
     * @param datetime2 结束时间
     * @return 对象集合
     */
    @Override
    public List<UserEntity> getUserTime(String datetime1, String datetime2) {
        return userMapper.getUserTime(datetime1,datetime2);
    }

    /**
     * 分页
     * @param offset 偏移量
     * @param limit 极限
     * @return 分页对象
     */
    @Override
    public ResponseEntity<PageUtils<List<UserEntity>>> getSelAll(int offset, int limit) {
        List<UserEntity> staffs = userMapper.getSelAll((offset - 1) * limit, limit);
        PageUtils<List<UserEntity>> pageList = new PageUtils<>();
        //改变当前页（设置为请求参数的页数）
        pageList.setCurrentPage(offset);
        //设置返回总页数
        pageList.setTotalCount(getTotalCount().getData());
        //设置返回内容
        pageList.setData(staffs);

        ResponseEntity<PageUtils<List<UserEntity>>> entity = ResponseEntity.success(pageList);
        return entity;
    }

    /**
     *
     * @return 总条数
     */
    @Override
    public ResponseEntity<Integer> getTotalCount() {
        int totalCount = userMapper.getTotalCount();
        ResponseEntity<Integer> entity = ResponseEntity.success(totalCount);
        return entity;
    }
}
